package com.hulu.yec.teach.testquestion.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.hulu.yec.common.Constant;
import com.hulu.yec.common.PageModel;
import com.hulu.yec.common.exception.DaoException;
import com.hulu.yec.teach.testquestion.dao.QuestionDao;
import com.hulu.yec.teach.testquestion.enetity.AddEntity;
import com.hulu.yec.teach.testquestion.enetity.ConditionEntity;
import com.hulu.yec.teach.testquestion.enetity.QuestionEntity;

@WebServlet("/teach/testquestion/QuestionServlet")
public class QuestionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final int PAGENO = 1;
	private static final int PAGESIZE = 10;
	QuestionDao questionDao = new QuestionDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("changeState".equals(command)) {
			changeState(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("updateInfo".equals(command)) {
			updateInfo(request, response);
		} else if ("preAdd".equals(command)) {
			preAdd(request, response);
		}else if ("add".equals(command)) {
			add(request, response);
		} else {
			throw new RuntimeException("请求非法!!!");
		}
	}

	/**
	 * 新增前跳转
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void preAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<ConditionEntity> nameList = new ArrayList<>();
		nameList = questionDao.getAllCoursename();
		request.setAttribute("nameList", nameList);
		request.getRequestDispatcher("/WEB-INF/view/teach/question/question-add.jsp").forward(
				request, response);
	}

	/**
	 * 新增试题
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AddEntity addEntity = new AddEntity();
		String question = request.getParameter("question");
		addEntity.setQuestion(question);
		String attachment = request.getParameter("attachment");
		addEntity.setAttachment(attachment);
		int question_type = Integer.parseInt(request.getParameter("question_type"));
		addEntity.setQuestion_type(question_type);
		String courseName = request.getParameter("courseName");
		int courseId = questionDao.getCourseID(courseName);
		addEntity.setCourse_id(courseId);
		String answer_a = request.getParameter("answer_a");
		addEntity.setAnswer_a(answer_a);
		String answer_b = request.getParameter("answer_b");
		addEntity.setAnswer_b(answer_b);
		String answer_c = request.getParameter("answer_c");
	    addEntity.setAnswer_c(answer_c);
		String answer_d = request.getParameter("answer_d");
		addEntity.setAnswer_d(answer_d);
		String answer = request.getParameter("answer");
		addEntity.setAnswer(answer);
		int difficulty = Integer.parseInt(request.getParameter("difficulty"));
		addEntity.setDifficulty(difficulty);
		String analysis = request.getParameter("analysis");
		addEntity.setAnalysis(analysis);
		String keywords = request.getParameter("keywords");
		addEntity.setKeywords(keywords);
		String memo = request.getParameter("memo");
		addEntity.setMemo(memo);
		int row = questionDao.add(addEntity);
		if (row == 1) {
			response.sendRedirect("QuestionServlet?command=list");
		} else {
			request.getRequestDispatcher(
					"/WEB-INF/view/teach/question/question-add.jsp")
					.forward(request, response);
		}
		
	}

	/**
	 * 修改试题
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updateInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AddEntity addEntity = new AddEntity();
		int id = Integer.parseInt(request.getParameter("id"));
		String question = request.getParameter("question");
		addEntity.setQuestion(question);
		String attachment = request.getParameter("attachment");
		addEntity.setAttachment(attachment);
		int question_type = Integer.parseInt(request.getParameter("question_type"));
		addEntity.setQuestion_type(question_type);
		String courseName = request.getParameter("courseName");
		int courseId = questionDao.getCourseID(courseName);
		addEntity.setCourse_id(courseId);
		String answer_a = request.getParameter("answer_a");
		addEntity.setAnswer_a(answer_a);
		String answer_b = request.getParameter("answer_b");
		addEntity.setAnswer_b(answer_b);
		String answer_c = request.getParameter("answer_c");
	    addEntity.setAnswer_c(answer_c);
		String answer_d = request.getParameter("answer_d");
		addEntity.setAnswer_d(answer_d);
		String answer = request.getParameter("answer");
		addEntity.setAnswer(answer);
		int difficulty = Integer.parseInt(request.getParameter("difficulty"));
		addEntity.setDifficulty(difficulty);
		String analysis = request.getParameter("analysis");
		addEntity.setAnalysis(analysis);
		String keywords = request.getParameter("keywords");
		addEntity.setKeywords(keywords);
		int state = Integer.parseInt(request.getParameter("state"));
		addEntity.setState(state);
		String memo = request.getParameter("memo");
		addEntity.setMemo(memo);
		int row = questionDao.updateInfo(id,addEntity);
		if (row == 1) {
			response.sendRedirect("QuestionServlet?command=list");
		} else {
			request.getRequestDispatcher(
					"/WEB-INF/view/teach/question/question-update.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 获取试题信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		List<QuestionEntity> questionList = new ArrayList<>();
		questionList = questionDao.getAllQuestionInfo(id);
		List<ConditionEntity> nameList = new ArrayList<>();
		nameList = questionDao.getAllCoursename();
		request.setAttribute("nameList", nameList);
		request.setAttribute("questionList", questionList);
		request.getRequestDispatcher(
				"/WEB-INF/view/teach/question/question-update.jsp").forward(
				request, response);
	}

	/**
	 * 分页条件查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws DaoException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConditionEntity conditionEntity = new ConditionEntity();
		int pageSize = PAGESIZE;
		int pageNo = PAGENO;
		// 获取参数
		String question = request.getParameter("question");
		String questionTypeStr = request.getParameter("question_type");
		if (StringUtils.isNoneBlank(questionTypeStr)) {
			int question_type = Integer.parseInt(questionTypeStr);
			conditionEntity.setQuestion_type(question_type);
		}

		String courseName = request.getParameter("courseName");
		String difficultyStr = request.getParameter("difficulty");
		if (StringUtils.isNoneBlank(difficultyStr)) {
			int difficulty = Integer.parseInt(difficultyStr);
			conditionEntity.setDifficulty(difficulty);
		}

		String state = request.getParameter("state");
		String pageSizeStr = request.getParameter("pageSize");
		String pageNoStr = request.getParameter("pageNo");
		if (request.getParameter("pageNo") != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		if (StringUtils.isNoneBlank(question)) {
			conditionEntity.setQuestion(question);
		}
		if (StringUtils.isNoneBlank(courseName)) {
			conditionEntity.setCourseName(courseName);
		}
		if (state != null && Integer.parseInt(state) != 0) {
			conditionEntity.setState(Integer.parseInt(state));
		}
		if (pageSizeStr != null && Integer.parseInt(pageSizeStr) != 0) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		// 调用逻辑
		List<ConditionEntity> nameList = new ArrayList<>();
		nameList = questionDao.getAllCoursename();
		PageModel<QuestionEntity> pageModel = questionDao.list(conditionEntity,
				pageNo, pageSize);
		// 转向
		request.setAttribute("nameList", nameList);
		request.setAttribute("conditionEntity", conditionEntity);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher(
				"/WEB-INF/view/teach/question/question-list.jsp").forward(
				request, response);
	}

	/**
	 * 状态更新
	 * 
	 * @param request
	 * @param response
	 * @throws DaoException
	 * @throws IOException
	 */
	private void changeState(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String inStr = request.getParameter("id");
		int id = 0;
		if (StringUtils.isNoneBlank(inStr)) {
			id = Integer.parseInt(inStr);
		} else {
			throw new RuntimeException("错误请求");
		}
		int reState = questionDao.getState(id);
		if (reState == 1) {
			reState = 2;
		} else if (reState == 2) {
			reState = 1;
		}
		questionDao.updateState(id, reState);
		// 转向
		response.setCharacterEncoding(Constant.ENCODING_UTF8);
		PrintWriter out = response.getWriter();
		int state = questionDao.getState(id);
		out.print(state);
		out.close();
	}
}